package com.scheduler.backend.controller;

import com.scheduler.backend.bl.ScheduleService;
import com.scheduler.backend.vo.ResponseVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.time.LocalDate;
import java.util.Calendar;
import java.util.Date;

@RestController()
@CrossOrigin
public class ScheduleController {

    @Autowired
    private ScheduleService scheduleService;

    @RequestMapping(value = "/doSchedule/{date}", method = RequestMethod.POST)
    @Operation(summary = "根据Date开始排程", description = "根据Date开始排程", method = "POST")
    @Parameter(name = "date", description = "日期", required = true)
    public ResponseVO doSchedule(@PathVariable("date") @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate date,
                                 HttpServletResponse response) {
        if (response != null) {
            response.setHeader("Access-Control-Allow-Origin", "*");
            response.setHeader("Cache-Control", "no-cache");
        }
        return scheduleService.startSchedule(date);
    }

    @RequestMapping(value = "/getShipment/{date}", method = RequestMethod.GET)
    @Operation(summary = "获取七天日均产量", description = "根据Date获取前七天的日均产量", method = "GET")
    @Parameter(name = "date", description = "日期", required = true)
    public ResponseVO getShipment(@PathVariable("date") @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate date,
                                  HttpServletResponse response) {
        if (response != null) {
            response.setHeader("Access-Control-Allow-Origin", "*");
            response.setHeader("Cache-Control", "no-cache");
        }
        return scheduleService.getShipment(date);
    }

    @RequestMapping(value = "/getPunctualRatioByTime/{date}", method = RequestMethod.GET)
    @Operation(summary = "根据Date获取准时率", description = "根据Date获取准时率", method = "GET")
    @Parameter(name = "date", description = "日期", required = true)
    public ResponseVO getPunctualRatioByTime(@PathVariable("date") @DateTimeFormat(pattern = "yyyy-MM-dd") Date date,
                                             HttpServletResponse response) {
        if (response != null) {
            response.setHeader("Access-Control-Allow-Origin", "*");
            response.setHeader("Cache-Control", "no-cache");
        }
        return scheduleService.getPunctualRatioByTime(date);
    }

    //按hour的暂时放着
    @RequestMapping(value = "/getResourceUseRatioByTimeScope/{startTime}/{endTime}", method = RequestMethod.GET)
    @Operation(summary = "根据开始结束时间获取一段时间内资源使用率", description = "根据开始结束时间获取一段时间内资源使用率", method = "GET")
    public ResponseVO getResourceUseRatioByTimeScope(
            @PathVariable("startTime") @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date startTime,
            @PathVariable("endTime") @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date endTime, HttpServletResponse response) {
        if (response != null) {
            response.setHeader("Access-Control-Allow-Origin", "*");
            response.setHeader("Cache-Control", "no-cache");
        }
        return scheduleService.getResourceUseRatioByTimeScope(startTime, endTime);
    }

    @RequestMapping(value = "/getResourceLoadRatioByDateScope/{startTime}", method = RequestMethod.GET)
    @Operation(summary = "根据开始结束时间获取一段时间内资源使用率", description = "根据开始结束日期获取一段时间内资源使用率", method = "GET")
    public ResponseVO getResourceLoadRatioByDateScope(
            @PathVariable("startTime") @DateTimeFormat(pattern = "yyyy-MM-dd") Date startTime,
            //@PathVariable("endTime") @DateTimeFormat(pattern = "yyyy-MM-dd") Date endTime,
            HttpServletResponse response) {
        if (response != null) {
            response.setHeader("Access-Control-Allow-Origin", "*");
            response.setHeader("Cache-Control", "no-cache");
        }
        //固定写死endTime为八天之后
        Calendar now = Calendar.getInstance();
        now.setTime(startTime);
        now.set(Calendar.DATE, now.get(Calendar.DATE) + 8);
        Date endTime = now.getTime();

        return scheduleService.getResourceLoadRatioByDateScope(startTime, endTime);
    }
}

